layui.use(['table', 'admin', 'ax','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem: '#storageDate'
        ,value:new Date()
    });

    /**
     * 运费成本差异管理
     */
    var DpkcQuery = {
        tableId: "dpkcQueryTable"
    };

    /**
     * 初始化表格的列
     */
    DpkcQuery.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'stockState', sort: true, title: '库存状态', style:'font-weight: 600;'},
            {field: 'warehouseName', sort: true, title: '所属仓库', style:'font-weight: 600;'},
            {field: 'areaName', sort: true, title: '所属库区', style:'font-weight: 600;'},
            {field: 'positionNumber', sort: true, title: '库位编码', style:'font-weight: 600;'},
            {field: 'positionType', sort: true, title: '库位类型', style:'font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: '商品编码', style:'font-weight: 600;'},
            {field: 'goodsName', sort: true, title: '商品名称', style:'font-weight: 600;'},
            {field: 'goodsPcode69', sort: true, title: '条码', style:'font-weight: 600;'},
            {field: 'kcNum', sort: true, title: '库存', style:'font-weight: 600;'},
            {field: 'kcUnit', title: '库存单位', style:'font-weight: 600;'},
            {field: 'jibenNum', sort: true, title: '基本单位数量', style:'font-weight: 600;'},
            {field: 'jibenUnit', title: '基本单位', style:'font-weight: 600;'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    DpkcQuery.search = function () {
        var queryData = {};
        queryData['storageDate'] = $("#storageDate").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['areaId'] = $("#areaId").val();
        queryData['positionNumber'] = $("#positionNumber").val();
        queryData['goodsNumber'] = $("#goodsNumber").val();
        table.reload(DpkcQuery.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    DpkcQuery.exportExcel = function () {
        var checkRows = table.checkStatus(DpkcQuery.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
            queryData['storageDate'] = $("#storageDate").val();
            queryData['warehouseId'] = $("#warehouseId").val();
            queryData['areaId'] = $("#areaId").val();
            queryData['positionNumber'] = $("#positionNumber").val();
            queryData['goodsNumber'] = $("#goodsNumber").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/dpkcQuery/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + DpkcQuery.tableId,
        url: Feng.ctxPath + '/dpkcQuery/list',
        page: true,
        limit: 60,
        limits:[40,60,120,240,360,480,1200],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: DpkcQuery.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        DpkcQuery.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        DpkcQuery.exportExcel();
    });


    //查询库区
    form.on('select(warehouseId)',function(){
        $("#areaId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicArea/Querylist',
            dataType:"json",   //返回格式为json
            async: false,
            data:{"areaWarehouseId":$("#warehouseId").val()},    //参数值
            type:"post",   //请求方式
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>请选择</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.areaNumber+"</option>";
                    });
                    $("#areaId").html(str);
                }
            }
        });
        form.render('select');
    });

});
